const fs = require('fs')
const qrcode = require('qrcode')

const opts = {
  errorCorrectionLevel: 'H',
  type: 'image/jpeg',
  rendererOpts: {
    quality: 0.3
  }
}

function createQrcode(data) {
  return new Promise((resolve, reject) => {
    qrcode.toDataURL(data, opts, function(err, url) {
      if (err) reject(err)
      resolve(url)
    })
  })
}

function renderFile(object, cb) {
  // 遍历输出字符
  let data = ''
  object.forEach((item) => {
    createQrcode(item.url).then((url) => {
      item.url = url
      data += `<section data-role="outer" style="font-size:16px;width: 100%;max-width: 600px;margin: 0 auto">
  <section class="article135" style="box-sizing: border-box;">
    <section class="_135editor active" data-tools="135编辑器" data-id="91008"
             style="border: 0 none; padding: 0; box-sizing: border-box;">
      <section style="box-shadow: 0 4px 8px 0 rgba(34,13,56,.5);margin:10px 5px;">
        <section
          style="    background-color: #337ab7;    background-image: linear-gradient(225deg,#337ab7,#00bae3);    box-sizing: border-box;    line-height: 0;    font-size: 16px;    color: #fff;    padding: 12px 20px;    font-weight: 700;">
          <p style="">
            ${item.company_name}
          </p>
        </section>

        <section style="padding: 2px 32px; box-sizing: border-box;">
          <section style="    line-height: 16px;    font-size: 14px;    color: #9b9ea0;">
            <section class="_135editor" data-tools="135编辑器" data-id="87782"
                     style="border: 0 none; padding: 0;">
              <section style="margin-right: auto; margin-left: auto; display: flex;justify-content: space-between;align-items: center">

                <section
                  style="padding-right: 5px; padding-left: 5px; overflow: hidden; position: static;">
                  <section
                    style="margin-top: 10px; margin-bottom: 10px;transform: rotate(3deg);-webkit-transform: rotate(3deg);-moz-transform: rotate(3deg);-o-transform: rotate(3deg);">
                    <section
                      style=";transform: rotate(-3deg);-webkit-transform: rotate(-3deg);-moz-transform: rotate(-3deg);-o-transform: rotate(-3deg);">
                      <section style="text-align: center; position: static;">
                        <img
                          src="${item.logo}"
                          style="z-index: -1; cursor: pointer; color: rgb(155, 158, 160); font-size: 14px; line-height: 17.9948px; text-align: center; white-space: normal; width: 64px; height: 64px;"
                          width="64" height="64"/>
                      </section>
                    </section>
                  </section>
                </section>

                <section
                  style="padding-top: 10px; padding-left: 10px; vertical-align: top; flex:0 0 62%;">
                  <p>
                    <strong>时间：${item.meet_day} ${item.meet_time}<br/></strong>
                  </p>
                  <section class="135brush" data-brushtype="text"
                           style="font-weight:bold;color: #454B57;"></section>
                  <section style="margin-top: 5px; position: static;">
                    <section class="135brush" style="text-align: justify; font-size: 13px;">
                      <p>
                        <strong><span
                          style="color: #9B9EA0; font-size: 14px; line-height: 17.99479103088379px; white-space: pre-wrap;">地点：${item.address}</span>。</strong>
                      </p>
                    </section>
                  </section>
                </section>

                <section style="margin-top: 8px; margin-bottom: 8px; position: static;">
                  <section style="height: 1px; background-color: rgb(160, 160, 160);"></section>
                </section>
              </section>
            </section>
          </section>
        </section>

        <section style="text-align: center">
          <img style="width:140px;height: auto; margin: 2px auto;" src="${item.url}"/>
        </section>

        <section style="
    background-color: #337ab7;
    background-image: linear-gradient(225deg,#337ab7,#00bae3);
    line-height: 40px;
    font-size: 14px;
    color: #fff;
    text-align: center;
    ">扫二维码了解详情
        </section>
      </section>
    </section>
    <p>
      <br/>
    </p>
  </section>
</section>
`
    })
  })
  setTimeout(() => {
    cb.header('Content-Type', 'text/html;charset=utf-8')
    return cb.end(data)
  }, 2500)
}

module.exports = renderFile
